package com.totoro.rabbitmq.client.listener;

import com.rabbitmq.client.Channel;
import com.totoro.rabbitmq.client.MQConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by Niki on 2018/4/11 15:05
 */
@Component
public class MessageListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageListener.class);
    @Bean
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        simpleRabbitListenerContainerFactory.setConnectionFactory(connectionFactory);

        //设置消息自动确认
        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.AUTO);
//        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        /*
            消费者一次性从队列中获取的消息个数，设置这儿参数提高RabbitMQ在高并发下处理消息的吞吐量
            也容易造成客户端消费不均衡的问题（此时将preferchCount设为1）

         */
//        simpleRabbitListenerContainerFactory.setPrefetchCount(10);

        return simpleRabbitListenerContainerFactory;
    }

    /**
     * 监听hello队列消息
     */
//    ,
//    @RabbitListener( containerFactory = "simpleRabbitListenerContainerFactory", queues = {MQConfig.QUEUE_HELLO})
    public void handleMessage_hello(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String msg = new String(message.getBody(), "utf-8");
        if (("unack".equals(msg))) {
            //不确认
            channel.basicNack(deliveryTag, false, true);
        } else {
            channel.basicAck(deliveryTag, false);
            System.out.println("hello 队列1收到消息：" + msg);
        }
    }

    //测试fanout多个consumer都能接受消息
    @RabbitListener(containerFactory = "simpleRabbitListenerContainerFactory", queues = {"EAS:PATCH:SERVICE:QUEUEA"})
    public void patchHadleMessage(Message message, Channel channel) {
        try {
            String msg = new String(message.getBody(), "utf-8");
            LOGGER.info("A收到补丁服务的消息为：{}", msg);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();

        }

    }

}
